/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.io;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.nio.charset.Charset;

/**
 * See {@link wtkx.Component} reader.  Synchronous input binding
 * permits authentication.
 * 
 * @author jdp
 */
public class UrlcInputStream
    extends java.io.FilterInputStream
{
    public final static Charset UTF8 = Charset.forName("UTF-8");


    protected final URLConnection connection;

    protected final int status;

    protected final String message;



    public UrlcInputStream(URLConnection c)
        throws java.io.IOException
    {
        super(c.getInputStream());
        this.connection = c;
        if (c instanceof HttpURLConnection){
            HttpURLConnection http = (HttpURLConnection)c;
            this.status = http.getResponseCode();
            this.message = http.getResponseMessage();
        }
        else {
            this.status = 0;
            this.message = null;
        }
    }


    public byte[] readContent()
        throws java.io.IOException
    {
        int len = this.getContentLength();
        if (0 < len){
            byte[] iob = new byte[len];
            int ofs = 0, read;
            while (0 < len){
                read = this.read(iob,ofs,len);
                len -= read;
                ofs += read;
            }
            return iob;
        }
        else
            return null;
    }
    public String readString()
        throws java.io.IOException
    {
        byte[] iob = this.readContent();
        if (null != iob){
            Reader reader = new InputStreamReader(new java.io.ByteArrayInputStream(iob),UTF8);

            StringBuilder string = new StringBuilder();
            char[] ciob = new char[0x200];
            int read;
            while (0 < (read = reader.read(ciob,0,0x200))){
                                
                string.append(ciob,0,read);
            }
            return string.toString();
        }
        else
            return null;
    }
    public int getContentLength(){
        return this.connection.getContentLength();
    }
    public boolean isHttp(){
        return (this.connection instanceof HttpURLConnection);
    }
    public boolean hasStatus(){
        return (0 < this.status);
    }
    public int getStatus(){
        return this.status;
    }
    public String getStatusMessage(){
        return this.message;
    }
    public URLConnection getConnection(){
        return this.connection;
    }
    public HttpURLConnection getHttp(){
        return (HttpURLConnection)this.connection;
    }
    public void close()
        throws java.io.IOException
    {
        try {
            super.close();
        }
        finally {
            if (this.connection instanceof HttpURLConnection)
                ((HttpURLConnection)this.connection).disconnect();
        }
    }
}
